package io.gitee.kotle.android

import android.app.Activity
import android.app.Application
import android.content.pm.PackageManager
import android.os.Handler
import android.os.Looper
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.pm.PackageInfoCompat
import coil.Coil
import coil.util.CoilUtils
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.tencent.mmkv.MMKV
import io.gitee.kotle.android.utils.*
import kotlinx.coroutines.MainScope
import java.util.concurrent.TimeUnit

/**
 * 供全局使用的常量
 */
object BaseGlobalConst {
    /**
     * 全局协程作用域
     */
    val mainScope by lazy { MainScope() }

    /**
     * 主线程的Handler
     */
    val mainHandler by lazy { Handler(Looper.getMainLooper()) }

    /**
     * gson对象
     */
    val gson: Gson by lazy {
        val builder = GsonBuilder()
        BaseLibraryConfig.onGsonBuilder?.invoke(builder) ?: builder.create()
    }

    /**
     * application对象
     */
    internal lateinit var internalApp: Application
    val app: Application
        get() = internalApp

    val packageInfo by lazy {
        app.packageManager.getPackageInfo(
            packageName,
            PackageManager.GET_ACTIVITIES
        )
    }

    /**
     * 当前app的包名
     */
    val packageName by lazy { app.packageName }

    /**
     * 当前的app的versionName
     */
    val versionName by lazy { packageInfo.versionName }

    /**
     * 当前的app的versionCode
     */
    val versionCode by lazy {
        PackageInfoCompat.getLongVersionCode(packageInfo)
    }

    /**
     * 通知栏对象
     */
    val notificationManagerCompat by lazy {
        NotificationManagerCompat.from(app)
    }

    /**
     * okHttpClient，通过[BaseLibraryConfig.onCreateOkhttpClient]可自定义
     */
    val okHttpClient by lazy {
        BaseLibraryConfig.onCreateOkhttpClient?.invoke()
            ?: OkHttpCertUtil
                .createCertOkHttpBuilder()
                .connectTimeout(3, TimeUnit.MINUTES)
                .readTimeout(3, TimeUnit.MINUTES)
                .writeTimeout(3, TimeUnit.MINUTES)
                .also {
                    BaseLibraryConfig.onOkhttpClientBuilder?.invoke(it)
                    //最后添加日志打印，保证打印最全
                    doOnlyDebug {
                        it.addNetworkInterceptor(::httpRequestLog)
                    }
                }
                .build()
    }

    /**
     * 图片加载的okhttp构建
     */
    val coilImageLoader by lazy {
        val builder = OkHttpCertUtil.createCertOkHttpBuilder()
        //必须要设置缓存
        val okHttpClient = BaseLibraryConfig.coilOkHttpClient?.invoke(builder) ?: builder.build()
        Coil.imageLoader(app)
            .newBuilder()
            .okHttpClient(okHttpClient)
            .respectCacheHeaders(false)
            .build()
    }

    /**
     * 腾讯的数据存储库
     */
    val mmkv by lazy {
        MMKV.initialize(app)
        MMKV.defaultMMKV()
    }

    /**
     * activityList集合
     */
    val activityList: List<Activity>
        get() = MvvmActivityLifecycleCallbacksUtil._activityList
}